package com.hy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hy.dto.regionDto.RegionTreeDto;
import com.hy.entity.City;
import com.hy.entity.District;
import com.hy.entity.Province;
import com.hy.mapper.ProvinceMapper;
import com.hy.service.CityService;
import com.hy.service.DistrictService;
import com.hy.service.ProvinceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProvinceServiceImpl extends ServiceImpl<ProvinceMapper, Province> implements ProvinceService {

    @Autowired
    private CityService cityService;
    
    @Autowired
    private DistrictService districtService;

    @Override
    public List<Province> getAllProvinces() {
        LambdaQueryWrapper<Province> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Province::getDeleteFlag, 0)
               .orderByAsc(Province::getProvincesName);
        return list(wrapper);
    }

    @Override
    public Province getProvinceById(Long id) {
        LambdaQueryWrapper<Province> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Province::getId, id)
               .eq(Province::getDeleteFlag, 0);
        return getOne(wrapper);
    }

    @Override
    @Transactional
    public boolean addProvince(Province province) {
        province.setDeleteFlag(0);
        province.setCreateTime(LocalDateTime.now());
        province.setUpdateTime(LocalDateTime.now());
        return save(province);
    }

    @Override
    @Transactional
    public boolean updateProvince(Province province) {
        province.setUpdateTime(LocalDateTime.now());
        return updateById(province);
    }

    @Override
    @Transactional
    public boolean deleteProvince(Long id) {
        LambdaUpdateWrapper<Province> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Province::getId, id)
                    .set(Province::getDeleteFlag, 1)
                    .set(Province::getUpdateTime, LocalDateTime.now());
        return update(updateWrapper);
    }

    @Override
    public List<RegionTreeDto> getRegionTree() {
        // 获取所有省份
        List<Province> provinces = getAllProvinces();
        
        // 获取所有城市
        List<City> cities = cityService.getAllCities();
        
        // 获取所有区县
        List<District> districts = districtService.getAllDistricts();
        
        // 转换为树形结构
        return provinces.stream().map(province -> {
            RegionTreeDto provinceDto = new RegionTreeDto();
            provinceDto.setValue(province.getId());
            provinceDto.setLabel(province.getProvincesName());
            
            // 获取该省份下的所有城市
            List<RegionTreeDto> cityDtos = cities.stream()
                .filter(city -> city.getParentId().intValue() == province.getId())
                .map(city -> {
                    RegionTreeDto cityDto = new RegionTreeDto();
                    cityDto.setValue(city.getId());
                    cityDto.setLabel(city.getCitiesName());
                    
                    // 获取该城市下的所有区县
                    List<RegionTreeDto> districtDtos = districts.stream()
                        .filter(district -> district.getParentId().intValue()== city.getId())
                        .map(district -> {
                            RegionTreeDto districtDto = new RegionTreeDto();
                            districtDto.setValue(district.getId());
                            districtDto.setLabel(district.getDistrictsName());
                            return districtDto;
                        })
                        .collect(Collectors.toList());
                    
                    cityDto.setChildren(districtDtos);
                    return cityDto;
                })
                .collect(Collectors.toList());
            
            provinceDto.setChildren(cityDtos);
            return provinceDto;
        }).collect(Collectors.toList());
    }
} 